const {
	sysConfig
} = require('@/api/system/config');
module.exports = {
	onLoad() {
		// 从Vuex获取用户参数（需通过Mixin注入`$store`）
		const userinfo = this.$store.state.user;
		console.log(userinfo)
		this.$t.mpShare = {
			title: '',
			path: `/pages/index/index?pid=${userinfo.pid || ''}`,
			imageUrl: 'https://avatar-1255804409.cos.ap-nanjing.myqcloud.com/xcx/banner/banner_3.png',
			share: true
		};

		if (!this.$t.mpShare.share) {
			uni.hideShareMenu();
		}
	},

	onShareAppMessage() {
		const userinfo = this.$store.state.user;
		return new Promise(async (resolve) => {
			let shareTitle = '';
			let shareImage = '';
			try {
				const response = await sysConfig('share');
				if (response?.code === 200 && Array.isArray(response.data)) {
					const shareTitleItem = response.data.find(item => item.configName === 'shareTitle');
					const shareImageItem = response.data.find(item => item.configName === 'shareImage');
					if (shareTitleItem && shareTitleItem.configValue) {
						shareTitle = shareTitleItem.configValue;
					}
					if (shareImageItem && shareImageItem.configValue) {
						shareImage = shareImageItem.configValue;
					}
				}
			} catch (e) {
				console.error('获取系统配置失败', e);
			}

			resolve({
				title: shareTitle,
				path: `/pages/index/index?pid=${userinfo.pid || ''}`,
				imageUrl: shareImage,
				share: true
			});
		});
	},

	// #ifdef MP-WEIXIN
	onShareTimeline() {
		return {
			title: this.$t.mpShare.title,
			// 提取路径中的参数（如`pid=123`）
			query: this.$t.mpShare.path.split('?')[1],
			imageUrl: this.$t.mpShare.imageUrl
		};
	}
	// #endif
};